package edu.uwm.capstone.diasmart.types;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.diasmart.SubTypeActivity;
import edu.uwm.capstone.diasmart.database.DatabaseHelper;
import edu.uwm.capstone.diasmart.database.Entry;
import edu.uwm.capstone.diasmart.database.SubType;
import edu.uwm.capstone.diasmart.util.AddEntryHelper;
import edu.uwm.capstone.diasmart.util.Common;
import edu.uwm.capstone.util.NumberUtil;
import edu.uwm.capstone.util.StringUtil;

/**
 * @author Palaniappan
 *
 */
public class EditableEntryTypeHelper extends Activity {
	public static final int BASE_ACTIVITY_SUBTYPE = 100;

	public static TableRow[] getStandardUserInterface(Activity paramActivity,
			EditableEntryType editableEntryType, int spinnerId, int j) {

		final Activity activity = paramActivity;
		final EditableEntryType eet = editableEntryType;
		TableLayout tableLayout = (TableLayout) paramActivity
				.getLayoutInflater().inflate(R.layout.entry_standard, null);
		TextView txtUnitOfMeasure = (TextView) tableLayout
				.findViewById(R.id.txtUnitOfMeasure);
		Spinner sprSubType = (Spinner) tableLayout
				.findViewById(R.id.sprSubType);
		sprSubType.setPromptId(R.string.subtype);
		loadSpinner(paramActivity,
				(TableRow) tableLayout.findViewById(R.id.trowSubType),
				sprSubType, editableEntryType);
		sprSubType.setId(spinnerId);
		sprSubType.setOnItemSelectedListener(new SubTypeOnItemSelectedListener(
				paramActivity, editableEntryType, txtUnitOfMeasure));
		sprSubType.setTag(editableEntryType);
		((Button) tableLayout.findViewById(R.id.btnAddSubType))
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent newIntent = new Intent(activity,
								SubTypeActivity.class);
						newIntent.putExtra("edu.uwm.capstone.diasmart.types",
								eet.getType());
						activity.startActivityForResult(newIntent,
								100 + eet.getType());
					}
				});
		if (txtUnitOfMeasure != null)
			txtUnitOfMeasure.setText(editableEntryType.getMeasurementUnit(
					new EntryContext(paramActivity),
					(SubType) sprSubType.getSelectedItem()));
		EditText edtValue = (EditText) tableLayout.findViewById(R.id.edtValue);
		Common.setNumericKeyboardType(paramActivity, edtValue,
				editableEntryType.getKeyboardType());
		edtValue.setId(spinnerId);
		TextView txtTitle = (TextView) tableLayout.findViewById(R.id.txtTitle);
		if (txtTitle != null)
			txtTitle.setText(EntryTypeFactory.getTypeString(editableEntryType
					.getType()));
		ArrayList<TableRow> trowList = new ArrayList<TableRow>();
		for (int i = tableLayout.getChildCount() - 1; i > 0; i--) {
			trowList.add(0, (TableRow) tableLayout.getChildAt(i));
			tableLayout.removeView(tableLayout.getChildAt(i));
		}
		return (TableRow[]) trowList.toArray(new TableRow[trowList.size()]);

	}

	private static void loadSpinner(Activity paramActivity,
			TableRow trowSubType, Spinner sprSubType,
			EditableEntryType editableEntryType) {
		DatabaseHelper dbh = new DatabaseHelper(paramActivity);

		try {
			SubType[] subTypes = dbh.getSubTypeDAO().getSubTypes(
					editableEntryType.getType());
			if (subTypes.length == 0) {
				sprSubType.setVisibility(Spinner.GONE);
				trowSubType.setVisibility(TableRow.GONE);
			}
			ArrayAdapter<SubType> subTypeAdapter = new ArrayAdapter<SubType>(
					paramActivity, android.R.layout.simple_spinner_item,
					subTypes);
			sprSubType.setAdapter(subTypeAdapter);
			subTypeAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			for (int i = 0; i < subTypes.length; i++) {
				if (subTypes[i].isUserDefault()) {
					sprSubType.setSelection(i);
				}
			}
		} catch (Exception e) {
			Log.e("Diasmart", e.getMessage());
		} finally {
			dbh.close();
		}

	}

	public static boolean handleOnActivityResult(Activity paramActivity,
			int type, int i, int paramInt1, int spinnerId, Intent paramIntent) {
		if (paramInt1 - 100 != type)
			return false;
		Spinner localSpinner = (Spinner) paramActivity.findViewById(spinnerId);
		DatabaseHelper localDatabaseHelper;
		if (localSpinner != null) {
			localDatabaseHelper = new DatabaseHelper(paramActivity);
			try {
				AddEntryHelper.updateSpinnerAdapter(localSpinner,
						R.string.subtype, localDatabaseHelper.getSubTypeDAO()
								.getSubTypes(paramInt1));
			} finally {
				localDatabaseHelper.close();
			}
		}
		return true;
	}

	public static long saveStandard(DatabaseHelper dbh,
			EditableEntryType entryType, Entry entry, View view,
			int editTextId, int spinnerId) {
		entry.setType(entryType.getType());
		Spinner localSpinner = (Spinner) view.findViewById(spinnerId);
		if (localSpinner.getVisibility() == 8)
			entry.setSubTypeId(dbh.getSubTypeDAO().getDefaultSubTypeId());

		EditText localEditText = (EditText) view.findViewById(editTextId);
		entry.setValue(new EntryContext(dbh.getContext()), localEditText
				.getText().toString());
		dbh.getEntryDAO().save(entry);

		SubType localSubType = (SubType) localSpinner.getSelectedItem();
		if (localSubType != null)
			entry.setSubTypeId(localSubType.getId());
		else
			entry.setSubTypeId(dbh.getSubTypeDAO().getDefaultSubTypeId());
		return entry.getId();
	}

	public static int validateStandard(EditableEntryType editableEntryType,
			View paramView, int i, int j) {
		EditText localEditText = (EditText) paramView.findViewById(i);
		return validateStandard(paramView.getContext(), localEditText.getText()
				.toString());
	}

	public static int validateStandard(Context context, String paramString) {
		if ((StringUtil.isEmpty(paramString))
				|| (StringUtil.isWhitespace(paramString)))
			return R.string.error_empty_value;
		else if (!NumberUtil.isNumber(paramString))
			return R.string.error_not_numeric_value;
		else
			return 0;
	}

	private static class SubTypeOnItemSelectedListener implements
			AdapterView.OnItemSelectedListener {
		private Context context;
		private TextView textView;
		private EntryType type;

		public SubTypeOnItemSelectedListener(Context context, EntryType type,
				TextView textView) {
			this.context = context;
			this.type = type;
			this.textView = textView;
		}

		public void onItemSelected(AdapterView<?> paramAdapterView,
				View paramView, int paramInt, long paramLong) {
			SubType subType = (SubType) paramAdapterView.getSelectedItem();
			if ((subType != null)
					&& (subType.getTypeId() != this.type.getType()))
				subType = null;
			String str = this.type.getMeasurementUnit(new EntryContext(
					this.context), subType);
			this.textView.setText(str);
		}

		public void onNothingSelected(AdapterView<?> paramAdapterView) {
			this.textView.setText("");
		}
	}

}
